import { Card, Button } from 'antd';
import { useState, useRef } from 'react';
import * as XLSX from 'xlsx';

const Index = () => {
  const ipt = useRef<HTMLInputElement>(null);

  const fn = (e: any) => {
    // console.log(e.target.files[0]);

    const file = e.target.files[0];

    const reader = new FileReader();
    reader.readAsBinaryString(file);
    reader.onload = () => {
      const workbook = XLSX.read(reader.result, { type: 'binary' });
      let res = workbook.Sheets['商品列表'];
      res = XLSX.utils.sheet_to_json(res);

      console.log(res);
    };
  };

  const handleClick = () => {
    ipt.current?.click();
  };

  return (
    <Card extra={<Button onClick={handleClick}>导入Excel</Button>}>
      <input type="file" onChange={fn} style={{ display: 'none' }} ref={ipt} />
    </Card>
  );
};

export default Index;
